/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.ui.detail;

import com.siro.pm.I18nManager;
import com.siro.pm.model.Attendance;
import com.siro.pm.model.provider.AttendanceProvider;
import com.siro.pm.ui.main.AttendancePage;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since AttendanceDetailPage.java v1.0
 */
public class AttendanceDetailPage extends VerticalLayout {
	private static final long serialVersionUID = 7811795256751559396L;

	private Window window;
	private AttendancePage attendancePage;
	private Attendance attendance;

	private ComboBox status;
	private TextField description;
	private TextField where;
	private TextField bankName;

	private Button button_Save;

	public AttendanceDetailPage() {
		super();
	}

	public void createContents() {
		setMargin(true);
		setSpacing(true);

		// base info
		GridLayout attendanceBaseInfo = new GridLayout();
		attendanceBaseInfo.setCaption(I18nManager
				.getMessage("attendance.base.info"));
		attendanceBaseInfo.setColumns(1);
		attendanceBaseInfo.setMargin(true);
		attendanceBaseInfo.setSpacing(true);
		attendanceBaseInfo.setWidth("100%");

		status = new ComboBox(AttendanceProvider.ATTENDANCE_PROPERTY_STATUS);
		String[] statusData = AttendanceProvider.ATTENDANCE_STATUS_DATA;
		for (int i = 0; i < statusData.length; i++) {
			status.addItem(statusData[i]);
		}
		status.setWidth("100%");
		status.setFilteringMode(Filtering.FILTERINGMODE_CONTAINS);
		status.setImmediate(true);
		status.addListener(new ValueChangeListener() {
			private static final long serialVersionUID = 1434331820631979074L;

			@Override
			public void valueChange(ValueChangeEvent event) {
				String value = (String) event.getProperty().getValue();
				if (!value.equals(Attendance.ATTENDANCE_STATUS_TRAVEL)) {
					where.setEnabled(false);
					bankName.setEnabled(false);
				} else {
					where.setEnabled(true);
					bankName.setEnabled(true);
				}

			}
		});
		attendanceBaseInfo.addComponent(status);

		description = new TextField(AttendanceProvider.ATTENDANCE_PROPERTY_DESC);
		description.setWidth("100%");
		attendanceBaseInfo.addComponent(description);

		where = new TextField(AttendanceProvider.ATTENDANCE_PROPERTY_WHERE);
		where.setWidth("100%");
		attendanceBaseInfo.addComponent(where);

		bankName = new TextField(
				AttendanceProvider.ATTENDANCE_PROPERTY_BANKNAME);
		bankName.setWidth("100%");
		attendanceBaseInfo.addComponent(bankName);

		// buttons
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setMargin(true);
		buttons.setSpacing(true);
		buttons.setWidth("100%");

		button_Save = new Button(I18nManager.getMessage("button.save"));
		button_Save.setWidth(50, Sizeable.UNITS_PIXELS);
		button_Save.addListener(new SaveClickListener());
		buttons.addComponent(button_Save);
		buttons.setComponentAlignment(button_Save, Alignment.MIDDLE_RIGHT);

		addComponent(attendanceBaseInfo);
		addComponent(buttons);
		initData();
	}

	private void initData() {
		status.select(attendance.getStatus());
		if (attendance.getDescription() != null)
			description.setValue(attendance.getDescription());
		if (attendance.getStatus().equals(Attendance.ATTENDANCE_STATUS_TRAVEL)) {
			if (attendance.getWhere() != null)
				where.setValue(attendance.getWhere());
			else if (attendancePage.lastWhere != null)
				where.setValue(attendancePage.lastWhere);
			if (attendance.getBankName() != null)
				bankName.setValue(attendance.getBankName());
			else if (attendancePage.lastBankName != null)
				bankName.setValue(attendancePage.lastBankName);
		}
		if (!attendance.getStatus().equals(Attendance.ATTENDANCE_STATUS_TRAVEL)) {
			where.setEnabled(false);
			bankName.setEnabled(false);
		}
	}

	class SaveClickListener implements ClickListener {
		private static final long serialVersionUID = -2851840644753744125L;

		public void buttonClick(ClickEvent event) {
			String attendanceStatus = (String) status.getValue();
			if (attendanceStatus.equals(Attendance.ATTENDANCE_STATUS_TRAVEL)) {
				if (where.getValue() == null
						|| where.getValue().equals("")
						|| where.getValue().equals(
								I18nManager
										.getMessage("attendance.where.errmsg"))) {
					where.setInputPrompt(I18nManager
							.getMessage("attendance.where.errmsg"));
					return;
				}
				if (bankName.getValue() == null
						|| bankName.getValue().equals("")
						|| bankName
								.getValue()
								.equals(I18nManager
										.getMessage("attendance.bankname.errmsg"))) {
					bankName.setInputPrompt(I18nManager
							.getMessage("attendance.bankname.errmsg"));
					return;
				}
			}
			attendance.setStatus(attendanceStatus);
			String sDesc = (String) description.getValue();
			if (sDesc != null)
				attendance.setDescription(sDesc);
			String sWhere = (String) where.getValue();
			attendance.setWhere(sWhere);
			String sBankName = (String) bankName.getValue();
			attendance.setBankName(sBankName);
			attendancePage.lastBankName = sBankName;
			attendancePage.lastWhere = sWhere;
			attendancePage.refresh();
			(window.getParent()).removeWindow(window);
		}
	}

	public Window getDeveloperWindow() {
		return window;
	}

	public void setWindow(Window window) {
		this.window = window;
	}

	public AttendancePage getAttendancePage() {
		return attendancePage;
	}

	public void setAttendancePage(AttendancePage attendancePage) {
		this.attendancePage = attendancePage;
	}

	public Attendance getAttendance() {
		return attendance;
	}

	public void setAttendance(Attendance attendance) {
		this.attendance = attendance;
	}

}
